package mysql

import (
	"gorm.io/gorm"
	"novel-start/novelrpc/novelrpc"
)

type Comment struct {
	gorm.Model
	UserId  int64  `gorm:"type:int(11);not null"`
	NovelId int64  `gorm:"type:int(11);not null"`
	Content string `gorm:"type:varchar(300);not null"`
}

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func CommentList(db *gorm.DB, in *novelrpc.CommentListRequest, list []*novelrpc.CommentList) []*novelrpc.CommentList {
	db.Debug().Model(&Comment{}).
		Select("comments.id as comment_id,novels.*,nick_name,head_image").
		Joins("left join novels on novels.id=comments.novel_id").
		Joins("left join users on users.id=comments.user_id").
		Where("comments.user_id = ?", in.UserId).
		Find(&list)
	return list
}
